<?php
//$_GET['id']
//$_GET['type']={node|group}
//$_GET['revision']=revision.id
//$_GET['encode']={0|1}
//$_GET['top'] select .. id<top

	include 'include/function.php';
	include_once 'include/revision.php';

	$history_id=$_GET['id'];
	$history_type=$_GET['type'];
	if(empty($history_id)||($history_type!='node'&&$history_type!='group'))redirect('./');
	$history_dtype=_d('rev_' . $history_type);

	$page_title=$site_title . " - $history_dtype $history_id " . _d('title_revision');
	include 'include/header.php';
	
	if($history_type=='node') {
		$history_perm=permission_node($_user_id,$history_id);
		if(!isset($history_perm[0])) {
			need_login();
			redirect('shownode.php?id=' . $history_id);
		}
	}
	
	$row=mysql_fetch_row(oj_query("select title from `$history_type` where id=$history_id"));
	$history_title=htmlspecialchars($row[0]);
	
	echo '<div class="problem_content" id="main_content">';
	echo '<center>';
		if($history_type=='group')
			echo "<p class='subtitle'><a href='group.php?action=show&id=$history_id'>$history_dtype $history_id $history_title</a></p>";
		else
			echo "<p class='subtitle'><a href='shownode.php?id=$history_id'>$history_dtype $history_id $history_title</a></p>";
	echo '</center>';
	if(!empty($_GET['revision'])) {
		$history_rev=$_GET['revision'];
		$row=mysql_fetch_row(oj_query("select node_id,type from revision where id=$history_rev"));
		if($row[0]==$history_id && $row[1]==$history_type) {
			$ret=oj_query("select `change` from revision where node_id=$history_id and type='$history_type' and id>=$history_rev order by id asc");
			$history_changes=array();
			while($row=mysql_fetch_row($ret)) {
				if(substr($row[0],0,2)=="\n\n") {
					$history_ori=substr($row[0],2);
					break;
				}
				array_push($history_changes,$row[0]);
			}
			if(!isset($history_ori)) {
				$row=mysql_fetch_row(oj_query("select description from `$history_type` where id=$history_id"));
				$history_ori=$row[0];
			}
			while(count($history_changes)>0) {
				$history_ori=recoverStringChanges($history_ori,array_pop($history_changes));
			}
			if($_GET['encode'])$history_ori='<pre>' . htmlspecialchars($history_ori) . '</pre>';
			echo $history_ori;
		} else {
			echo 'bad request';
		}
	}
	echo '</div>';

	$history_top=$_GET['top'];
	if(!empty($history_top))$history_top='and id<' . $history_top;
	draw_tablelist_head(array(_d('rev_id'),_d('rev_editor'),_d('rev_edit_time'),_d('rev_length'),_d('rev_comment')));
	$ret=oj_query('select id,user_id,rev_time,length,comment from revision '
				. "where node_id=$history_id and type='$history_type' $history_top order by id desc limit 10 ");
	$count=0;
	$last_id=0;
	while($row=mysql_fetch_assoc($ret)) {
		echo '<tr>';
			echo "<td><a href='history.php?id=$history_id&type=$history_type&revision={$row['id']}&encode=1&top={$_GET['top']}'>{$row['id']}</a></td>";
			echo '<td align="center">' . user_id_to_name_link($row['user_id']) . '</td>';
			echo "<td align='center'>{$row['rev_time']}</td>";
			echo "<td>{$row['length']}</td>";
			echo '<td>' . htmlspecialchars($row['comment']) . '</td>';
		echo '</tr>';
		$count++;
		$last_id=$row['id'];
	}
	echo '</table>';
	echo '<center>';
		echo "[<a href='history.php?id=$history_id&type=$history_type'>Top</a>]&nbsp;";
		if($count==10)
			echo "[<a href='history.php?id=$history_id&type=$history_type&top=$last_id'>Next page</a>]&nbsp;";
	echo '</center>';
	
	include 'include/footer.php';
	
?>
